<?php
class UsersController extends AppController {

	var $name = 'Users';
        
        public function beforeFilter() {
            parent::beforeFilter();
            $this->Auth->allow('admin_login', 'dashboard', 'offline');
        }
        
        public function add() {
            if (!empty($this->data)) {
                $this->User->create();
                if ($this->User->save($this->data)) {
                    $this->Session->setFlash('User created!');
                    $this->redirect(array('action'=>'login'));
                } else {
                    $this->Session->setFlash('Please correct the errors');
                }
            }
            $this->set('groups', $this->User->Group->find('list'));
        }
	
	public function offline() {
		//echo $isActive;
        }

	function admin_index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('usr', $this->User->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
                        $this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('Đã thêm người dùng', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$cities = $this->User->City->find('list');
		$this->set(compact('groups', 'cities'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã được lưu', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$cities = $this->User->City->find('list');
		$this->set(compact('groups', 'cities'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
        
        public function login() {
		$this->layout = "login";
            $user = $this->Auth->user();
            if (!empty($user)) {
                $groupName = $this->User->Group->field('name', array('Group.id'=>$this->Auth->user('group_id')));
                $this->redirect(array('action'=>strtolower($groupName)));
            }
        }
        
        public function admin_login() {
		
            $this->redirect(array('action'=>'login', 'admin'=>false));
        }
        
        public function logout() {
          $this->redirect($this->Auth->logout());
        }
        
        public function dashboard() {
            $groupName = $this->User->Group->field('name', array('Group.id'=>$this->Auth->user('group_id')));
            $this->redirect(array('action'=>strtolower($groupName)));
        }
        
        public function user() {
        }
        
        public function manager() {
		$this->redirect(array('controller'=>'CityDetails', 'manager'=>true));
        }
        
        public function administrator() {
            $this->redirect(array('controller'=>'cities', 'action'=>'index', 'admin' => true));
        }
	
	function manager_changepassword()
	{
	
		$user = $this->Auth->user();
		$user = $this->User->read(null, $user['User']['id']);
		if (!empty($this->data)) {
			if($user['User']['password'] != $this->Auth->password($this->data['User']['old_password']))
			{
				$this->Session->setFlash(__('Mật khẩu đang dùng không đúng.', true));
			}
			else
			{
				if($this->data['User']['new_password'] != $this->data['User']['new_password_confirm'])
				{
					$this->Session->setFlash(__('Xác nhận mật khẩu mới không đúng', true));
				}
				else
				{
					if($this->data['User']['new_password'] != '')
					{
						$this->data['User']['password'] = $this->Auth->password($this->data['User']['new_password']);
						$this->data['User']['confirm_password'] = $this->data['User']['password'];
						if ($this->User->save($this->data)) {
							$this->Session->setFlash(__('Mật khẩu đã được thay đổi thành công', true));
							$this->redirect(array('controller'=>'CityDetails'));
						} else {
							$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
						}
					}
					else
					{
						$this->Session->setFlash(__('Xin nhập mật khẩu mới.', true));
					}					
				}				
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $user['User']['id']);
			$this->data['User']['confirm_password'] = $this->data['User']['password'];
		}		
		
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}
}
